import axiosOps from 'axios';
import cookie from 'js-cookie';
import { hasIn } from 'lodash';
import Vue from 'vue';
import { urlDownload } from '../const/utils.js';

export const $loadFileSimply = param => {
  console.log(param, '8888888888888888')
  let token = cookie.get('token');
  let tenantId = cookie.get('tenantId');
  param.url += param.url.indexOf('?') > -1 ? '&' : '?';
  param.url += `tenantId=${tenantId}&_=${new Date().getTime()}`;
  return new Promise((resolve, reject) => {
    axiosOps({
      url: param.url,
      method: param.method,
      data: param.data,
      params: param.params,
      responseType: 'arraybuffer',
      headers: {
        Authorization: `Bearer ${token}`,
        tenantId: tenantId
      },
      timeout: param.timeout ? param.timeout : 5000
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(() => {
        Vue.$notify.error({
          title: '错误提示',
          message: '下载文件失败'
        });
      });
  });
};

// 文件模板下载
// 默认url是委托书的模板
export const $loadModelFile = (
  url = '/guoke-channel-aggregation-center/api/v1/systemdictionary?code=WTTEMPLATE',
  fn = () => { }
) => {
  let token = cookie.get('token');
  let tenantId = cookie.get('tenantId');
  axiosOps({
    url: url,
    method: 'get',
    headers: {
      Authorization: `Bearer ${token}`,
      tenantId: tenantId
    }
  })
    .then(res => {
      let url = hasIn(res, ['data', 'payload', 'dicValue']) ? res.data.payload.dicValue : '';
      urlDownload(url);
      fn();
    })
    .catch(() => {
      Vue.$notify.error({
        title: '错误提示',
        message: '获取模板地址失败'
      });
    });
};
